package com.ofo.services.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ofo.dao.BikePasswordDao;
import com.ofo.dao.MarksDao;
import com.ofo.global.CacheSelectData;
import com.ofo.model.BikePassword;
import com.ofo.model.Marks;
import com.ofo.services.IndexActionServices;

@Service("indexActionServices")
public class IndexActionServicesImpl implements IndexActionServices {
	@Autowired
	private MarksDao barksDao;

	@Autowired
	private BikePasswordDao bikePasswordDao;

	public Marks getMarks(Integer marksId) {
		Marks marks = CacheSelectData.getBikeCache(marksId);
		if (marks == null) {
			marks = barksDao.getMarks(marksId);
			if (marks != null) {
				marks.setBikePasswords(bikePasswordDao.getBikePasswordByMarks(marksId));
				CacheSelectData.addtBikeCache(marksId, marks);
			}
		}
		return marks;
	}

	@Override
	public boolean addMarks(Integer marksId, String password,String createUser) {
		try {
			Integer.parseInt(password);
		} catch (Exception e) {
			return false;
		}
		Marks marks = CacheSelectData.getBikeCache(marksId);
		if (marks == null) {
			marks = new Marks();
			marks.setMarks(marksId);
			marks.setCreateUser(createUser);
			barksDao.addMarks(marks);
			BikePassword bikePassword = new BikePassword();
			bikePassword.setMarks(marksId);
			bikePassword.setPassword(password);
			bikePassword.setCreateUser(createUser);
			bikePasswordDao.addBikePassword(bikePassword);
			marks.setBikePasswords(Arrays.asList(bikePassword));
			CacheSelectData.addtBikeCache(marksId, marks);
			return true;
		}
		return false;
	}
}
